var app = angular.module('signApp', []);
app.controller('signController', function($scope, $http) {
	$scope.mobileExist = true;
	$scope.codeOK = false;
	$scope.countdown = 60;
	$scope.yhxy = true;
	$scope.user = {
		id : "",
		mobile : "18521355675",
		code : "949948",
		password : "123456",
		password2 : "123456",
		name : "FML",
		company : "Accenture",
		number : "512366199501015533",
		office : "外滩18号"
	};
	$scope.clear = function(item) {
		$scope.user[item] = '';
	};
	$scope.mobileCheck = function() {
		var mobile = $scope.user.mobile.trim();
		if (mobile.length != 11 || isNaN(mobile)) {
			return;
		}
		$http({
	        method: 'POST',
	        url: '../query.do',
	        data: { "queryID": "user.S001", "condition": [mobile] },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		if (response.data.count == 1) {
	    			if (response.data.dataList[0].count == 0) {
	    				$scope.mobileExist = false;
		    		} else {
		    			alert("该手机号已存在！");
		    		}
	    		}
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
	};
	$scope.sendCode = function() {
		var mobile = $scope.user.mobile.trim();
		if (mobile.length != 11 || isNaN(mobile)) {
			return;
		}
		$http({
	        method: 'POST',
	        url: '../sendCode.do',
	        data: { "mobile": mobile, "category": "0" },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		if (response.data.code == "0") {
	    			$("#sendCodeButton").attr("disabled", true);
	    			$("#sendCodeButton").text($scope.countdown + "s后重新发送");
	    			var interval = setInterval(function(){
	    				$scope.countdown--;
	    				if ($scope.countdown == 0) {
	    					window.clearInterval(interval)
	    					$scope.countdown = 60;
	    					$("#sendCodeButton").text("发送验证码");
	    	    			$("#sendCodeButton").attr("disabled", false);
	    				} else {	    					
	    					$("#sendCodeButton").text($scope.countdown + "s后重新发送");
	    				}
	    			}, 1000);
	    		} else {
	    			alert("验证码发送失败，请重试！");
	    		}
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
	};
	$scope.codeCheck = function() {
		var mobile = $scope.user.mobile.trim();
		if (mobile.length != 11 || isNaN(mobile)) {
			return;
		}
		var code = $scope.user.code.trim();
		if (code.length != 6 || isNaN(code)) {
			return;
		}
		$http({
	        method: 'POST',
	        url: '../query.do',
	        data: { "queryID": "code.S001", "condition": [mobile, "0"] },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		if (response.data.count == 1) {
	    			if (response.data.dataList[0].code == code) {
	    				$scope.codeOK = true;
		    		} else {
		    			$scope.codeOK = false;
		    		}
	    		}
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
	};
	$scope.signup = function() {
		var data = {
			mobile : $scope.user.mobile.trim(),
			password : $scope.user.password.trim(),
			name : $scope.user.name.trim(),
			company : $scope.user.company.trim(),
			number : $scope.user.number.trim(),
			office : $scope.user.office.trim()
		};
		if (data.mobile.length != 11 || isNaN(data.mobile)) {
			alert("请输入正确的手机号!");
			return;
		}
		if ($scope.mobileExist) {
			alert("该手机号已存在!");
			return;
		}
		var code = $scope.user.code.trim();
		if (code.length != 6 || isNaN(code)) {
			alert("验证码不正确!");
			return;
		}
		if (!$scope.codeOK) {
			alert("验证码不正确!");
			return;
		}
		if (data.password.length < 6 || data.password.length > 12) {
			alert("请输入6-12位密码!");
			return;
		}
		var password2 = $scope.user.password2.trim();
		if (password2 != data.password) {
			alert("密码不一致!");
			return;
		}
		if (data.name.length == 0) {
			alert("请输入真实姓名!");
			return;
		}
		if (data.company.length == 0) {
			alert("请输入所在公司名称!");
			return;
		}
		if (data.number.length == 0) {
			alert("请输入身份证号码!");
			return;
		}
		if (data.office.length == 0) {
			alert("请输入公司所在详细地址!");
			return;
		}
		if (!$scope.yhxy) {
			alert("请阅读并同意《用户协议》!");
			return;
		}
		
		data.table = "user";
		data.action = "create";
		data.append = "all";
		$http({
	        method: 'POST',
	        url: '../save.do',
	        data: data,  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		if (response.data.code == '0') {
	    			alert("注册成功！");
	    			window.location.href = 'index.html';
	    		} else {
	    			alert("注册失败！请重试！");
	    		}
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
	};
	$scope.signin = function() {
		window.location.href = 'index.html';
	};
});